
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout; 

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class ImageEditor extends JDialog implements MouseListener, ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	int thumbCount = 8;
	
	ImageUploader parrent;
	ArrayList<ImagePreview> thumbnails = new ArrayList<ImagePreview>();
	
	ArrayList<EditedImage> images = new ArrayList<EditedImage>();
	GridBagConstraints constr = new GridBagConstraints();
	
	int curr = 0;
	int sel = 0;


	JPanel thumbPanel;
	
	JButton browse;
	JButton upload;

		
	BufferedImage blank256II;
	EditedImage blank256;
	String uploadUrl;
	
	Dimension resizedDimension;
	
	ImageEditor(ImageUploader parrent) {		
		super();
		
		String wi = parrent.getParameter("width_r");
		String he = parrent.getParameter("height_r");		
		int w = Integer.parseInt(wi);
		int h = Integer.parseInt(he);
		this.resizedDimension = new Dimension(w, h);
		
		this.setResizable(false);
		this.parrent = parrent;
		this.loadBlanks();
		this.setLayout(new GridBagLayout());
	
		this.setThumbnails();
		this.addControlls();
		pack();

		
		this.display();
		}
	
	private void loadBlanks() {
		return;
//		try {		
//			this.blank256II = ImageIO.read(ImageEditor.class.getResourceAsStream( "256x.png" ));
//			this.blank256 = new EditedImage(this.blank256II, new Dimension(256, 256), "png", "blank");
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//			}
		}
	JScrollPane panel;
	private void setThumbnails() {		

		thumbPanel = new JPanel();
		thumbPanel.setLayout(null);
		//lab.setPreferredSize(new Dimension(800, 180));
		
		
		//thumbPanel.setLayout(new GridLayout(0,3));
		//lab.setPreferredSize(new Dimension(1000, 1000));
		
		panel = new JScrollPane(thumbPanel);
		
		panel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		panel.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		panel.setPreferredSize(new Dimension(665, 552));
		thumbPanel.setPreferredSize(new Dimension(656, 540));
		thumbPanel.setAlignmentX(LEFT_ALIGNMENT);
		panel.setViewportView(thumbPanel);
		
		this.constr.gridx = 0;
		this.constr.gridy = 0;			
		this.add(panel, this.constr);
				
		}
	
	private void  addThumbnail(EditedImage img) {
		int count = images.size();			
		ImagePreview newThumb = new ImagePreview(new Dimension(160, 180), img, false);
		int visina = (int)count/4 * 184 +2;
		newThumb.setBounds(count%4 * 164 + 2,visina, 160,180);		
		newThumb.setAlignmentX(LEFT_ALIGNMENT);
		newThumb.setAlignmentY(LEFT_ALIGNMENT);
		this.thumbnails.add(newThumb);		
		newThumb.addMouseListener(this);
			
		newThumb.progressPanel.doClick();
		thumbPanel.add(newThumb);
		thumbPanel.setPreferredSize(new Dimension(164*4, (visina+ 184)));		
		thumbPanel.revalidate();
		//this.panel.scrollRectToVisible(new Rectangle(164*4, 184* count/4));
		
		
		
		
		//panel.add(newThumb, this.constr);			
			
		}
	private void addControlls() {
		JPanel controllsPannel = new JPanel();
		controllsPannel.setLayout(new FlowLayout());
		controllsPannel.setAlignmentY(RIGHT_ALIGNMENT);		
		this.addBrowseControlls(controllsPannel);
		
		this.constr.gridx = 0;
		this.constr.gridy = 1;
		this.constr.anchor = GridBagConstraints.NORTH;
		this.constr.fill = GridBagConstraints.HORIZONTAL;
		//controllsPannel.setAlignmentX(RIGHT_ALIGNMENT);
		
		this.add(controllsPannel, this.constr);
		}
	

	
	private void addBrowseControlls(JPanel controlls) {
		controlls.add(Box.createRigidArea(new Dimension(450,10)));
		browse = new JButton("Browse");
		browse.setAlignmentX(RIGHT_ALIGNMENT);
		browse.addActionListener(this);		
		controlls.add(browse);
		

		upload = new JButton("Upload");
		upload.setAlignmentX(RIGHT_ALIGNMENT);
		upload.addActionListener(this);
		controlls.add(upload);
		}
	

	@Override
	public void actionPerformed(ActionEvent e) {
 

		if (e.getSource() == browse) {
			SyncFlag flag = new SyncFlag();
						
			
        	JFileChooser fileDialog = new JFileChooser();
        	fileDialog.setMultiSelectionEnabled(true);
        	
        	FileFilter imageFilter = new FileNameExtensionFilter("JPG, PNG", new String[] {"JPG", "JPEG", "PNG"});
        	fileDialog.setFileFilter(imageFilter);
        	
        	int retval = fileDialog.showOpenDialog(null);
            if (retval == JFileChooser.APPROVE_OPTION) {
            	//... The user selected a file, get it, use it.
            	JPanel loadingBack = new JPanel();;
            	loadingBack.setLayout(null);            	
            	loadingBack.setBounds(this.thumbPanel.getWidth()/2 - 120, this.thumbPanel.getHeight()/2-100,200,200);
            	
//            	JLabel doing = new JLabel();
//            	ImageIcon doingImage = new ImageIcon(ImageEditor.class.getResource("ajax-loader.gif"));
//    			doing.setIcon(doingImage);
//    			doing.setBounds(25,12,60,60);
//    			loadingBack.add(doing);
            	
    			
    			JLabel loading = new JLabel("Loading .");
    			loading.setBounds(0,70,160,40);
    			loadingBack.add(loading);
    			
    			this.thumbPanel.add(loadingBack);
                File[] imgFile = fileDialog.getSelectedFiles();
                
                ButtonPressed browse = new ButtonPressed(this, flag, imgFile);
                browse.start();
                
                while (!browse.done) {
                	flag.get("editor");
                	loading.setText(browse.loaded);
                	loadingBack.update(loadingBack.getGraphics());
                	
                	}
                	
    			
    			this.thumbPanel.remove(loadingBack);
    			
    			EditedImage img[] = browse.images;
    			if (img != null) {
    				for (int i=0; i<img.length; i++) {
    					this.addThumbnail(img[i]);
    					images.add(img[i]);
    					}					
    				}
            	}	
			
			
			thumbPanel.revalidate();
			thumbPanel.repaint();
			this.display();
			}		
		else if (e.getSource() == upload) {
			this.setVisible(false);
			}
		
		}
	
	public EditedImage[] getImages() {
		int number=0;
		for (int i=0; i<this.images.size(); i++)
			if (((EditedImage)this.images.get(i)).upload)
				number ++;
				
		EditedImage[] uImages = new EditedImage[number];
		
		int count = 0;
		for (int i=0; i<this.images.size(); i++)
			if (((EditedImage)this.images.get(i)).upload) {
				uImages[count] = (EditedImage)this.images.get(i);
				count ++;
				}
		panel.setPreferredSize(new Dimension(600, (images.size()%3 +1)*200));
		this.pack();
		
		return uImages;
		}

	
	
	public void display() {
//		int j=0;
//		for (int i = curr; (i<curr+this.thumbCount) && (i<images.size()) ; i++) {
//			EditedImage img = images.get(i);
//			thumbnails[j].setImage(false, img);
//			thumbnails[j].setEnabled(true);
//			thumbnails[j].progressPanel.setSelected(img.upload);
//			j++;
//			}
//		for (;j<this.thumbCount; j++) {
//			thumbnails[j].setImage(false, this.blank256);
//			thumbnails[j].setEnabled(false);
//			}
//		
			
		}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	

	}


